package com.bignerdranch.android.ecar.Activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.bignerdranch.android.ecar.Adapter.ProvinceAdapter;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;
import com.bignerdranch.android.ecar.sortlistview.ChooseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class AddCarActivity extends BaseActivity implements View.OnClickListener {

    private ImageButton add_car_back;
    private ImageButton doubt;
    private ImageButton series_back;
    private EditText car_number;
    private ImageButton province_but;
    private ImageButton code_doubt;
    private EditText code_num;
    private Button query;
    private PopupWindow mWindow;
    private EditText car_se;
    private TextView province;
    private TextView car_type;

    private static final int SERIES_REQUEST_CODE = 0;
    private RadioButton auto_little;
    private RadioButton little;
    private RadioButton big_truck;
    private RadioButton big_car;
    private RadioButton middle_car;
    private RadioButton bus;
    private RadioButton big_bus;
    private RadioGroup mRadioGroup;
    private boolean flag = true;
    private boolean flag2 = true;
    private int count = 15;
    private String [] location ={"京","津","冀","晋","蒙","辽","吉","黑","沪","苏","浙","皖","闽","赣","鲁","豫","鄂","湘","粤","桂","琼",
            "渝","川","贵","云","藏","陕","甘","青","宁","新","港","澳","台"};


    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_car);
        initView();



    }

    private void initView() {

        add_car_back = (ImageButton) findViewById(R.id.add_car_back);
        province = (TextView) findViewById(R.id.province);
        car_type = (TextView) findViewById(R.id.car_type);
        car_type.setOnClickListener(this);
        car_se = (EditText) findViewById(R.id.car_se);
        add_car_back.setOnClickListener(this);
        doubt = (ImageButton) findViewById(R.id.doubt);
        doubt.setOnClickListener(this);
        series_back = (ImageButton) findViewById(R.id.series_back);
        series_back.setOnClickListener(this);
        car_number = (EditText) findViewById(R.id.car_number);
        car_number.setOnClickListener(this);
        province_but = (ImageButton) findViewById(R.id.province_but);
        province_but.setOnClickListener(this);
        code_doubt = (ImageButton) findViewById(R.id.code_doubt);
        code_doubt.setOnClickListener(this);
        code_num = (EditText) findViewById(R.id.code_num);
        code_num.setOnClickListener(this);
        query = (Button) findViewById(R.id.query);
        query.setOnClickListener(this);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case SERIES_REQUEST_CODE:
                String series = data.getStringExtra("carseries");
                car_se.setText(series);
                break;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_car_back:
                finish();
                break;
            case R.id.doubt:
                showExplain(v, R.layout.doubt_dialog1);
               /* Dialog dialog = new AlertDialog.Builder(this)
                        .setView(R.layout.doubt_dialog1)
                        .create();
                dialog.show();*/
                break;
            case R.id.series_back:
                Intent intent = new Intent(this, ChooseActivity.class);
                startActivityForResult(intent, SERIES_REQUEST_CODE);
                break;
            case R.id.province_but:
                showExplain(v,R.layout.province_dialog);
               /* Dialog dialog = new AlertDialog.Builder(this).setIcon(R.drawable.logo)
                        .setTitle("请选择省份")
                        .setSingleChoiceItems(location, count, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                province.setText(location[which]);
                                count = which;
                                dialog.dismiss();
                            }
                        }).create();
                dialog.show();*/

                break;
            case R.id.code_doubt:
                showExplain(v, R.layout.doubt_dialog2);
                break;
            case R.id.query:
                Intent intent1 = new Intent(AddCarActivity.this,FaultResultActivity.class);

                startActivity(intent1);
                finish();
                break;
            case R.id.car_type:
                showPopu(v, R.layout.car_type_dialog);
                break;

        }
    }

    /**
     * 初始化popuwindow，要和show popuwindow分开
     * 如果每次点击都要初始化popuwindow，会降低效率
     * 每次重新加载时会重置选择状态，所以只要在初次运行的时候加载就可以了
     * @param popu
     */
    private void initPopView( int popu){
        View v = getLayoutInflater().inflate(popu, null);
        mWindow = new PopupWindow(v, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_rectangle));
        mRadioGroup =(RadioGroup) v.findViewById(R.id.rg_cartype);
        little = (RadioButton) v.findViewById(R.id.little);
        little.setChecked(true);//默认选中状态
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.auto_little:
                        car_type.setText(R.string.auto_little);
                        mWindow.dismiss();
                        break;
                    case R.id.little:
                        car_type.setText(R.string.little);
                        mWindow.dismiss();
                        break;
                    case R.id.big_car:
                        car_type.setText(R.string.big_car);
                        mWindow.dismiss();
                        break;
                    case R.id.big_truck:
                        car_type.setText(R.string.big_truck);
                        mWindow.dismiss();
                        break;
                    case R.id.middle_car:
                        car_type.setText(R.string.middle_car);
                        mWindow.dismiss();
                        break;
                    case R.id.bus:
                        car_type.setText(R.string.bus);
                        mWindow.dismiss();
                        break;
                    case R.id.big_bus:
                        car_type.setText(R.string.big_bus);
                        mWindow.dismiss();
                        break;

                }
            }
        });
    }

    private void showPopu(View view, int popu) {
        switch (popu){
            case R.layout.car_type_dialog:
                if (flag){
                    initPopView(popu);
                    flag = false;
                }
                break;

        }


        mWindow.setFocusable(true);
        mWindow.setOutsideTouchable(true);
        mWindow.setTouchable(true);
        setAlpha(0.6f);
//        mWindow.showAsDropDown(anchor,0,0);
        mWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        mWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1);
            }
        });
    }
    private void initGrid(View v){
        mGridView = (GridView) v.findViewById(R.id.province_grid);
        ProvinceAdapter mAdapter = new ProvinceAdapter(AddCarActivity.this,location);
        if (mAdapter!=null){
            mGridView.setAdapter(mAdapter);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    TextView textView=(TextView)view.findViewById(R.id.province_item);
                    province.setText(textView.getText());
                    mWindow.dismiss();
                }
            });
        }


    }
    private void showExplain(View view, int popu){
        View v = getLayoutInflater().inflate(popu, null);
        if(popu==R.layout.province_dialog){
            initGrid(v);
        }
        mWindow = new PopupWindow(v, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_rectangle));

        mWindow.setFocusable(true);
        mWindow.setOutsideTouchable(true);
        mWindow.setTouchable(true);
        setAlpha(0.6f);
//        mWindow.showAsDropDown(anchor,0,0);
        mWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        mWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1);
            }
        });
//        mWindow=null;
    }

    private void setAlpha(float alpha) {
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.alpha = alpha;
        window.setAttributes(params);
    }


}
